<?php

namespace Wap\Controller;
use \Index\Controller\CommonController;

class IndexController extends CommonController {
    //protected $_needLogin = true;

    //首页
    public function index(){
        C('LAYOUT_ON',false);
        //首页推荐展示
        $idsa = \Common\Model\SettingModel::getValue('jind-tj');
        $jindTj = D('Common/Book')->getList("book_id IN ({$idsa})",'',0,3,'',false);
        $this->assign('bookTj',$jindTj['lists']);
        $this->display();
    }

    //登陆
    public function DOlogin(){
        if(!$this->checkVerify($_POST['verify'])){
            $this->error("验证码错误");exit;
        }

        if(empty($_POST['username']) || empty($_POST['__hash__']) || empty($_POST['password'])){
            $this->display();exit;
        }
        $username = I('post.username');
        $pass = I('post.password');
        $Pass = new \Common\Model\MemberPamModel();
        if($Pass->autoCheckToken($_POST)){
            $info = $Pass->login($username);
            $pass = genSecretStr($pass,$info['password_account']);
            if($info['login_password'] === $pass){
                $minfo = \Common\Library\COM\MemberDo::getLoginInfo($info['member_id'],'login');
                session('_id',$minfo['member_id']);
                session('_info',$minfo);
                //p($minfo);
                $y = date('Y',$minfo['last_login']);
                $m = date('m',$minfo['last_login']);
                $d = date('d',$minfo['last_login']);
                if(date('Y',time()) > $y || date('m',time()) > $m || date('d',time()) > $d){
                    \Common\Library\COM\MemberDo::addMemberPoint($info['member_id'],10,'每天登录奖励10个积分');
                }
                //p($y);p($m);p($d);p(date('Y-m-d',$minfo['last_login']));exit;
                $this->success('登录成功');exit;
            }
        }
        $this->error('用户名或密码错误');
    }


    //注册
    public function DOregist(){
        if(!$this->checkVerify($_POST['verify'])){
            $this->error("验证码错误");exit;
        }

        $login_account = I('post.username');
        $password = I('post.password');
        if(isUsername($login_account)){
            $Members = new \Common\Model\MembersModel();
            if(!$Members->autoCheckToken($_POST)){
                $this->error('注册失败');exit;
            }
            if(\Common\Library\COM\MemberDo::checkUsername($login_account)){
                $this->error('该用户名:'.$username.' 已注册');exit;
            }

            $data = array('regtime'=>  time(),'reg_ip'=>  get_client_ip(),'last_login'=>  time());
            $Members = new \Common\Model\MembersModel();
            if($member_id = $Members->add($data)){
                $Pass = new \Common\Model\MemberPamModel();
                $password_account = getRandStr(10);
                $login_password = genSecretStr($password,$password_account);
                $data = array(
                    'member_id'=>$member_id,
                    'login_account'=>$login_account,
                    'login_password'=>$login_password,
                    'password_account'=>$password_account,
                );
                if($Pass->add($data)){
                    session('_id',$member_id);
                    session('_info',\Common\Library\COM\MemberDo::getLoginInfo($info['member_id'],'login'));
                    \Common\Library\COM\MemberDo::addMemberPoint($member_id,10,'每天登录奖励10个积分');
                    $this->success('注册成功');exit;
                }
            }
        }
        $this->error('注册信息有误');
    }

    public function login(){
        if($this->_id > 0){
            $this->redirect('Index/index');exit;
        }

        $this->assign('title',C('SYS_NAME').'会员登录');
        $this->display();
    }

    public function regist(){
        if($this->_id > 0){
            $this->redirect('Index/index');exit;
        }
        $this->assign('title',C('SYS_NAME').'会员注册');
        $this->display();
    }

        //榜单
    public function newph(){
        $t = I('request.t');
        $cate_id = I('get.cate_id');
        if($cate_id > 0){
            $where = "`cate_id` = '{$cate_id}'";
        }
        $Book = new \Common\Model\BookModel();
        $Cate = new \Common\Model\BookCateModel();
        $this->limit = 9;
        $this->offset = ($this->page-1) * $this->limit;
        $books = $Book->getList($where,'brow_num DESC', $this->offset, $this->limit,$fields ='');
        $this->assign('totalPage',ceil($books['total']/$this->limit));

        $this->assign('books',$books);
        $this->assign('cates',$Cate->where("`is_view` = 2")->field('cate_id,alias')->select());
        $month = null;$all = null;$week = null;
        switch ($t) {
            case 'month':
                $month = 'class="tab-nav-cur"';
                break;
            case 'all':
                $all = 'class="tab-nav-cur"';
                break;
            default:
                $week = 'class="tab-nav-cur"';
                break;
        }
        $url = empty($cate_id)?'':'/cate_id/'.$cate_id;
        $url .= '/page/'.$this->page;
        $title = '<span class="h-title"><a '.$week.' href="'.C('WAP_SITE').'/newph'.$url.'/t/week.html" >周榜</a></span>';
        $title .= '<span class="h-title" ><a '.$month.' href="'.C('WAP_SITE').'/newph'.$url.'/t/month.html" >月榜</a></span>';
        $title .= '<span class="h-title"><a '.$all.' href="'.C('WAP_SITE').'/newph'.$url.'/t/all.html" >总榜</a></span>';
        $this->assign('title',$title);
        $this->assign('cate_id',empty($cate_id)?0:$cate_id);
        $this->assign('t',empty($t)?'week':$t);
        $this->display('bangdan');
    }

    //等级说明
    public function lvList(){
        $cate_id = I('get.cate_id');
        $where = "status = 'ing'";
        if($cate_id > 0){
            $where .= " AND `cate_id` = '{$cate_id}'";
        }
        $Book = new \Common\Model\BookModel();
        $this->limit = 9;
        $this->offset = ($this->page-1) * $this->limit;
        $books = $Book->getList($where,'brow_num DESC', $this->offset, $this->limit,$fields ='');
        $this->assign('totalPage',ceil($books['total']/$this->limit));

        $this->assign('books',$books);
        $this->assign('title','全本小说');
        $this->assign('cate_id',empty($cate_id)?0:$cate_id);
        $this->assign('cates',D('Common/BookCate')->where('`is_view` = 2')->select());
        $this->display('quanben');
    }

    //阅读记录
    public function readLog(){
        $log = json_decode(cookie(),true);
        //p((cookie()));exit;

        $this->assign('title','阅读记录(保存最近阅读10本书)');
        $this->display('readlog');
    }

}
